<script setup lang="ts">
import { ref } from "vue";
import { userEmployee } from "./utils/hook";
import Tree from "./tree.vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import EditPen from "@iconify-icons/ep/edit-pen";
import Delete from "@iconify-icons/ep/delete";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Employee"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  jobList,
  deptTree,
  dataList,
  pagination,
  onSearch,
  resetForm,
  openDialog,
  handleLeave,
  deptLoading,
  handleDelete,
  onSelectDept,
  handleSizeChange,
  handleCurrentChange,
  openUploadDialog
} = userEmployee();
</script>

<template>
  <div class="flex justify-between">
    <Tree
      ref="treeRef"
      class="min-w-[200px] mr-2"
      :treeData="deptTree"
      :treeLoading="deptLoading"
      @tree-select="onSelectDept"
    />
    <div class="w-[calc(100%-200px)]">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
      >
        <el-form-item label="职务" prop="jobId">
          <el-select
            v-model="form.jobId"
            class="!w-[200px]"
            clearable
            placeholder="请选择职务"
          >
            <el-option
              v-for="(item, index) in jobList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工名称：" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入员工名称"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <DictOption
            v-model="form.status"
            code="Employee_Status"
            type="select"
            placeholder="请选择状态"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon(Search)"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar title="员工列表" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <Auth value="import_employee">
            <el-button
              type="primary"
              :icon="useRenderIcon('material-symbols:upload')"
              @click="openUploadDialog()"
            >
              导入表格
            </el-button>
          </Auth>
          <Auth value="add_employee">
            <el-button
              type="primary"
              :icon="useRenderIcon(AddFill)"
              @click="openDialog()"
            >
              新增员工
            </el-button>
          </Auth>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            align-whole="center"
            showOverflowTooltip
            table-layout="auto"
            :loading="loading"
            :size="size"
            adaptive
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="pagination"
            :paginationSmall="size === 'small'"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <Auth value="edit_employee">
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(EditPen)"
                  @click="openDialog('编辑', row)"
                >
                  编辑
                </el-button>
              </Auth>
              <Auth value="employee_leave">
                <el-popconfirm
                  :title="`是否确认离职员工${row.name}`"
                  @confirm="handleLeave(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :size="size"
                      :icon="useRenderIcon('pepicons-pop:leave')"
                    >
                      离职
                    </el-button>
                  </template>
                </el-popconfirm>
              </Auth>
              <Auth value="delete_employee">
                <el-popconfirm
                  :title="`是否确认删除员工名称为${row.name}的这条数据`"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :size="size"
                      :icon="useRenderIcon(Delete)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </Auth>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
